package com.maisonliberte.crawller;

import java.util.ArrayList;
import java.util.List;

import com.maisonliberte.entity.MakerInfo;
import com.maisonliberte.entity.Staff;
import com.maisonliberte.entity.StaffInfo;
import com.maisonliberte.exceptions.CrawlingException;
import com.maisonliberte.util.HtmlUtil;

public class StyleStoreBrandCrawler extends AbstractBrandCrawler{

	@Override
	public String getNextPage(String data) throws Exception {
		String nextUrl = "";

		try{
			List<String> liList = new ArrayList<String>();
			String pageNavi = data.substring(data.indexOf("<div class=\"pageNation\">"));
			pageNavi = pageNavi.substring(0, pageNavi.indexOf("<!-- //.pageNation --></div>"));
			
			while(pageNavi.indexOf("<li") > -1){
				String tmp = pageNavi.substring(pageNavi.indexOf("<li"));
				tmp = tmp.substring(0, tmp.indexOf("</li>") + 5);
				liList.add(tmp);
				pageNavi = pageNavi.replace(tmp, "");	
			}
			
			for(String li : liList){
				if(li.indexOf("次のページへ") > -1){
					if(HtmlUtil.getATag(li) != null){
						nextUrl = HtmlUtil.getHrefOfATag(HtmlUtil.getATag(li), "class=\"next\"");
						break;
					}
				}
			}
			
			if(!nextUrl.isEmpty()){
				String nextPage = nextUrl.replace("/blogs/", "");
				nextUrl = String.format("%s/%s", this.url, nextPage); 
			}
			
		} catch(Exception ex){
			throw new CrawlingException("getNextPage", ex);
		}
		
		return nextUrl;
	}

	@Override
	public void analyzeParsedData(String data) throws Exception {
		
		try{
			String body = data.substring(data.indexOf("<ul class=\"cList\">"));
			body = body.substring(0, body.indexOf("<div class=\"pageNation\">"));
			
			while(body.indexOf("<li>") > -1){
				String tmp = body.substring(body.indexOf("<li>"));
				tmp = tmp.substring(0, tmp.indexOf("</li>")+5);
				body = body.replace(tmp, "");
				parsingDataList.add(tmp);
			}
			
		}catch(Exception ex){
			throw new CrawlingException("analyzeParsedData", ex);
		}
	}

	@Override
	public Staff getBrandInfo(String data) throws Exception {
		Staff staff = null;
		try{
			//<li>
			//<div class="img">
			//<a href="/blog/T01262/">
			//<img src="http://img.enfactory.jp/stylestore/product/result/T01262_result.jpg" alt="Myers Hiroko" title="Myers Hiroko" width="100" height="100" /></a>
			//</div>
			//<div class="icon"><img src="/img/creator.gif" alt="つくり手" title="つくり手" width="44" height="17" /></div>
			//<h2><a href="/blog/T01262/">Myers Hiroko</a></h2>
			//<div class="kana">まいやーず ひろこ</div>
			//<div class="brand">m.h. Art Studio</div>
			//グラフィックデザイナーとして音楽ソフト（http://trainer.thetamusic.com/）のデザインを続ける一方、その対極にあるような自然素材に惹…				</li>
			String body = data;
			
			String detailUrl = HtmlUtil.getHrefOfATag(HtmlUtil.getATag(body), ">");
			String img = body.substring(body.indexOf("<div class=\"img\">"));
			img = img.substring(0, img.indexOf("</div>") + 6);
			img = HtmlUtil.getImageSrcOfImgTag(img, "alt=");
			body = body.substring(body.indexOf("</div>") + 6);
			
			String name = body.substring(body.indexOf("<h2>"));
			name = name.replace("<h2>", "").replace("</h2>", "");
			name = HtmlUtil.getTextLabelOfATag(HtmlUtil.getATag(name));
			body = body.substring(body.indexOf("</h2>") + 5);
			
			body = body.substring(body.indexOf("<div class=\"brand\">"));
			String brand = body.substring(0);
			brand = brand.substring(0, body.indexOf("</div>") + 6);
			brand = HtmlUtil.getTextLabelOfDivTag(brand);
			body = body.substring(body.indexOf("</div>") + 6);
			String comment = body.substring(0).replace("</li>", "");
			
			if(data.indexOf("/img/creator.gif") > -1){
				staff = new MakerInfo();
			}else{
				staff = new StaffInfo();
			}
			staff.setId(detailUrl.replace("/blog/", "").replace("/", ""));
			staff.setName(name.replace("\t", ""));
			staff.setPic_src(img);
			staff.setBrand(brand.replace("\t", ""));
			staff.setComment(comment.replace("\t", ""));	
			
		}catch(Exception ex){
			throw new CrawlingException("getBrandInfo", ex);
		}
		
		return staff;
	}

	

}
